package leetcode;

import util.ListNode;

/**
 * @Project : algorithm-learning-java
 * @Package : leetcode
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/1/18 17:22
 */
public class Q0082 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return null;
        ListNode h = new ListNode(-0x3f3f3f3f, head);
        ListNode p = h;
        ListNode q = head;
        while (q.next != null) {
            if (q.val != q.next.val) {
                p = q;
                q = q.next;
            } else {
                ListNode s = q.next.next;
                while (s != null && s.val == q.val) {
                    s = s.next;
                }
                p.next = s;
                if (s == null) break;
                q = s;
            }
        }
        return h.next;
    }
}
